# 监控指标封装
"""
根据实际项目编写，此处为示例代码
"""
from prometheus_client import start_http_server, Counter, Histogram, Gauge, Info
import os

# 定义指标
test_success_counter = Counter(
    'test_success_total',
    'Number of successful tests',
    ['plugin']
)

test_failure_counter = Counter(
    'test_failure_total',
    'Number of failed tests',
    ['plugin']
)

response_time_histogram = Histogram(
    'response_time_seconds',
    'Response time distribution',
    ['plugin'],
    buckets=(0.1, 0.5, 1.0, 2.0, 5.0, float('inf'))
)

concurrent_requests_gauge = Gauge(
    'concurrent_requests',
    'Current number of concurrent requests'
)

system_info = Info('system_info', 'System information')


def setup_prometheus(app):
    # 启动metrics端点
    start_http_server(8000)

    # 添加Flask请求中间件
    @app.before_request
    def before_request():
        concurrent_requests_gauge.inc()

    @app.after_request
    def after_request(response):
        concurrent_requests_gauge.dec()
        return response

    # 初始化系统信息
    system_info.info({
        'version': 'v1.0.0',
        'env': os.getenv('ENVIRONMENT', 'development')
    })
